package dao;

import java.util.List;

import javax.persistence.CacheRetrieveMode;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.Rorder;
import model.RorderPK;
import model.Route;
import model.Tourist;
import util.EntityManagerUtil;

public class RorderDao {

	public static Rorder newRorder(String temail, String rname) throws Exception {
		Route route = RouteDao.getRouteNoCache(rname);
		Tourist tourist = TouristDao.getTouristByEmail(temail);
		Rorder rorder = new Rorder();
		RorderPK pk = new RorderPK();
		pk.setTEmail(tourist.getEmail());
		pk.setRName(route.getRname());
		rorder.setId(pk);
		return rorder;
	}

	public static Rorder getRorder(Rorder Rorder) {
		EntityManager em = EntityManagerUtil.getEntityManager();
		Rorder RorderFind = em.find(Rorder.class, Rorder.getId());
		if (RorderFind != null) {
			em.close();
			return RorderFind;
		} else
			return null;
	}

	public static List<Rorder> reRorder(List<Rorder> l1) {
		EntityManager em = EntityManagerUtil.getEntityManager();
		em.refresh(l1);
		for (Rorder c : l1) {
			em.refresh(c);
		}
		em.close();
		return l1;
	}

	// 查找信息无缓存
	public static Rorder getRorderNoCache(Rorder Rorder) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Rorder c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Rorder RorderFind;
		try {
			RorderFind = (Rorder) query.setParameter("id", Rorder.getId()).getSingleResult();
			em.close();
			return RorderFind;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			em.close();
			return null;
		}
	}

	public static boolean getRorderNoCacheForBool(Rorder Rorder) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Rorder c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Rorder RorderFind;
		try {
			RorderFind = (Rorder) query.setParameter("id", Rorder.getId()).getSingleResult();
			em.close();
			return true;
		} catch (Exception e) {
			em.close();
			return false;
		}
	}

	// ��Ӿ��㶩����Ϣ
	public static boolean addRorder(Rorder Rorder) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			if (!getRorderNoCacheForBool(Rorder)) {
				em.getTransaction().begin();
				em.persist(Rorder);
				em.getTransaction().commit();
				em.close();
				return true;
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	public static boolean updateRorder(Rorder rorder) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Rorder RorderFind = getRorderNoCache(rorder);
			System.out.println("fg2"+RorderFind.getId().getRName());
			System.out.println("fg2"+RorderFind.getId().getTEmail());
				if (RorderFind != null) {
					Rorder upRorder = em.find(Rorder.class, rorder.getId());
					em.getTransaction().begin();
					upRorder.setStatus(rorder.getStatus());
					em.persist(upRorder);
					em.getTransaction().commit();
					em.close();
				return true;
			} else {
				System.out.println("失败");
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	// ɾ���Ƶ궩����Ϣ success
	public static boolean delRorder(Rorder rorder) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Rorder rorderFind = getRorderNoCache(rorder);
			if (rorderFind != null) {
				Rorder delRorder = em.find(Rorder.class, rorder.getId());
				em.getTransaction().begin();
				em.remove(delRorder);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}

	public static void main(String args[]) {
		Rorder rorder = new Rorder();
		RorderPK pk = new RorderPK();
		pk.setTEmail("22");
		pk.setRName("22");
		rorder.setId(pk);
		rorder.setMoney(11);

		try {
			addRorder(rorder);
			// delRorder(rorder);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
